package com.tgh.gles2.c5.tunnel;
import com.tgh.gles2.R;

import android.app.Activity;
import android.opengl.GLES20;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class GLActivity extends Activity implements OnTouchListener{

    private MySurfaceView surfaceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_c5_tunnel);
        surfaceView = (MySurfaceView) findViewById(R.id.esview);
        findViewById(R.id.forward).setOnTouchListener(this);
        findViewById(R.id.back).setOnTouchListener(this);
    }
    @Override
    protected void onResume() {
        super.onResume();
        surfaceView.onResume();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        surfaceView.onPause();
    }
    
    int id;
    boolean flag=true;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction()==MotionEvent.ACTION_DOWN) {
            id=v.getId();
            flag=true;
            new Thread(longClick).start();
            return true;
        }else if (event.getAction()==MotionEvent.ACTION_UP) {
            flag=false;
            return true;
        }
        return false;
    }
    
    private  Runnable UIRunnable=new Runnable() {
        @Override
        public void run() {
            switch (id) {
                case R.id.forward:
                    surfaceView.moveForward();
                    break;
                case R.id.back:
                    surfaceView.moveBack();
                    break;
                default:
                    break;
            }
        }
    };
    
    private Runnable longClick =new Runnable() {
        @Override
        public void run() {
            while (flag) {
                runOnUiThread(UIRunnable);
               try {
                Thread.sleep(50);
                } catch (InterruptedException e) {}
            }
        }
    };
}
